package com.fatjay.gettop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

public class allBoard extends ExpandableListActivity {
	ExpandableListAdapter mAdapter;
	Map<String, String> boardlist = new HashMap<String, String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mAdapter = new MyExpandableListAdapter();
		getAllBoard();
        setListAdapter(mAdapter);
        registerForContextMenu(getExpandableListView());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Sample menu");
        menu.add(0, 0, 0, R.string.menu_show);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		System.out.println("clicked!");
		String url = boardlist.get( ((MyExpandableListAdapter)mAdapter).children.get(groupPosition).get(childPosition) );
		//return super.onChildClick(parent, v, groupPosition, childPosition, id);
		return true;
	}

	private String getAllBoard() {
    	String myString = null;
    	//StringBuffer sff = new StringBuffer();
    	((MyExpandableListAdapter)mAdapter).groups.add("Lily System");
    	((MyExpandableListAdapter)mAdapter).groups.add("Nanjing University");
    	((MyExpandableListAdapter)mAdapter).groups.add("Mother Land");
    	((MyExpandableListAdapter)mAdapter).groups.add("Computer Tech");
    	((MyExpandableListAdapter)mAdapter).groups.add("Schoolar Ship");
    	((MyExpandableListAdapter)mAdapter).groups.add("Culture and Art");
    	((MyExpandableListAdapter)mAdapter).groups.add("Sports");
    	((MyExpandableListAdapter)mAdapter).groups.add("Feelings");
    	((MyExpandableListAdapter)mAdapter).groups.add("News");
    	((MyExpandableListAdapter)mAdapter).groups.add("Spaces");
    	((MyExpandableListAdapter)mAdapter).groups.add("School Info");
    	((MyExpandableListAdapter)mAdapter).groups.add("Scociety Groups");
    	((MyExpandableListAdapter)mAdapter).groups.add("Cold Board");
    	
    	for (int i=0; i<13; i++) {
    		ArrayList<String> sub = new ArrayList<String>();
    		try {
        		Document doc = Jsoup.connect("http://bbs.nju.edu.cn/bbsboa?sec=" + i).get();
        		Elements links = doc.select("tr");
        		for (Element link : links) {
        			Elements board = link.select("a");
        			if (board.size() == 0)
        				continue;
        			String url = board.get(1).attr("abs:href");
        			String name = board.get(1).text();
        			boardlist.put(name, url);
        			System.out.println(name);
        			//sub = 
        			sub.add(name);
        		}
        		((MyExpandableListAdapter)mAdapter).children.add(sub);
        	} catch (Exception e) {
        		myString = e.getMessage();
        		e.printStackTrace();
        	}
    	}
    	
    	return myString;
    }

	public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        public ArrayList<String> groups = new ArrayList<String>();
        public ArrayList<ArrayList<String>> children = new ArrayList<ArrayList<String>>();
        
        public Object getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64);

            TextView textView = new TextView(allBoard.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
        
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

        public int getGroupCount() {
            return groups.size();
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }
    }
}
